import wx, wx.html2, os, winreg as _winreg
from plotly.graph_objs import Scatter,Layout
import plotly
import plotly.offline as py
import numpy as np
import plotly.graph_objs as go

import plotly as py
import plotly.figure_factory as FF

# ----------pre def
pyplt = py.offline.plot
#setting offilne

def create_table():
    data_matrix = [['国家', '年份', '人口'],
    ['中国',2000, 1267430000],
    ['美国', 2000, 282200000],
    ]

    table = FF.create_table(data_matrix)
    pyplt(table, filename='simple_table', show_link=False)


class MyBrowser(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, None, -1, "My Frame", size=(300, 300))
        # 这里需要打开所有权限


        self.key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
                                   r"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                                   0, _winreg.KEY_ALL_ACCESS)
        try:
            # 设置注册表python.exe 值为 11000(IE11)
            _winreg.SetValueEx(self.key, 'python.exe', 0, _winreg.REG_DWORD, 0x00002af8)
        except:
            # 设置出现错误
            print('error in set value!')
        self.browser = wx.html2.WebView.New(self, style=0)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, evt):
        print('close')
        # 用完取消注册表设置
        _winreg.DeleteValue(self.key, 'python.exe')
        # 关闭打开的注册表
        _winreg.CloseKey(self.key)
        evt.Skip()

def plot_plotly():
    N = 100
    random_x = np.linspace(0, 1, N)
    random_y0 = np.random.randn(N) + 5
    random_y1 = np.random.randn(N)
    random_y2 = np.random.randn(N) - 5

    # Create traces
    trace0 = go.Scatter(
        x=random_x,
        y=random_y0,
        mode='markers',
        name='markers'
    )
    trace1 = go.Scatter(
        x=random_x,
        y=random_y1,
        mode='lines+markers',
        name='lines+markers'
    )
    trace2 = go.Scatter(
        x=random_x,
        y=random_y2,
        mode='lines',
        name='lines'
    )
    data = [trace0, trace1, trace2]
    py.plot(data)

if __name__ == '__main__':
    create_table()
    app = wx.App()
    frame = MyBrowser()
    #plot_plotly()
    frame.browser.LoadURL(os.path.realpath("../../config/bkt_result.html"))
    frame.Show()
    app.MainLoop()